// 导入
require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.less')
require('../font/iconfont.css')
require('../css/login.less')

// 引入weui
require('weui')
const weui = require('weui.js')

// 引入axios
const axios = require('axios')

document.addEventListener('DOMContentLoaded', function () {
    // 获取元素
    const tel = document.querySelector('#tel') //手机号
    const password = document.querySelector('#password') //密码
    const submitBtn = document.querySelector('#submitBtn') //密码
    submitBtn.addEventListener('click', function () {
        if (!/^1[3-9][0-9]{9}$/.test(tel.value)) {
            msg.textContent = '手机号不正确'
            return;
        } else if (!/^[a-zA-Z]\w{5,11}$/.test(password.value)) {
            msg.textContent = '密码不正确'
            return;
        } else {
            msg.textContent = ''
            // 给服务器发送请求
            const url = 'http://139.9.177.51:8099/users/login'
            axios.post(url, {
                "account": tel.value,
                "password": password.value
            }).then(function (res) {
                if (res.data.status === 0) {
                    // console.log(res);
                    // 将userId存入本地存储
                    localStorage.setItem('userId', res.data.data.user.userId)

                    weui.toast('登录成功', {
                        duration: 2000,
                        className: 'custom-classname',
                        callback: function () {

                            location.href = './home.html'
                        }
                    });
                } else {
                    weui.alert('账号或密码错误');
                }
            })
        }
    })

})